package PrjHttpUtils

import (
	"dgo/goutils/utils"
	"fmt"
	"net/http"
	"sync"
)

var (
	// SubscribeCenter
	sc_map sync.Map

	// MultiGroupTask
	mg_map sync.Map

	// queuetask_map
	qt_map sync.Map

	// blockcount map
	block_map sync.Map
)

func AddSubscribleToDebugInfo(sc *utils.SubscribeCenter) {
	sc_map.Store(sc.Id, sc)
}

func AddMultiGroupTaskToDebugInfo(obj *utils.MultiGroupTask) {
	mg_map.Store(obj.Id, obj)
}

func AddQueueTaskToDebugInfo(obj *utils.QueueTask) {
	qt_map.Store(obj.Id, obj)
}

func AddBlockerToDebugInfo(obj *utils.BlockCounter) {
	block_map.Store(obj.Id, obj)
}

func OnHttpProjDebug(w http.ResponseWriter, r *http.Request) {

	fmt.Fprint(w, "=======block counter\r\n")
	block_map.Range(func(key, value interface{}) bool {
		if obj, ok := value.(*utils.BlockCounter); ok {
			fmt.Fprintf(w, "%s Info\r\n", obj.Id)
			fmt.Fprint(w, obj.String())
		}
		return true
	})

	fmt.Fprint(w, "=======subscrible center\r\n")
	sc_map.Range(func(key, value interface{}) bool {
		if sc, ok := value.(*utils.SubscribeCenter); ok {
			fmt.Fprintf(w, "%s(%s) Info\r\n", sc.Id, sc.Desc)
			fmt.Fprint(w, sc.StatusString())
		}

		return true
	})

	fmt.Fprint(w, "=======multi group task\r\n")
	mg_map.Range(func(key, value interface{}) bool {
		if obj, ok := value.(*utils.MultiGroupTask); ok {
			fmt.Fprintf(w, "%s Info\r\n", obj.Id)
			fmt.Fprint(w, obj.StatusString())
			fmt.Fprint(w, "\r\n")
		}
		return true
	})

	fmt.Fprint(w, "=======queue task\r\n")
	qt_map.Range(func(key, value interface{}) bool {
		if obj, ok := value.(*utils.QueueTask); ok {
			fmt.Fprintf(w, "%s Info\r\n", obj.Id)
			fmt.Fprint(w, obj.String())
		}
		return true
	})
}

func RegisterProjDebugHandleFuncs() {
	http.HandleFunc("/proj/debug", OnHttpProjDebug)
}

func RegisterProjHttpLogFileBrowse() {
	StartFileService("/log/", "log/")

}
